<?php

namespace backend\widgets;

use dmstr\widgets\Menu;
use Yii;
use yii\helpers\Html;

/**
 * 后台左侧菜单
 *
 * @package backend\widgets
 */
class SidebarMenu extends Menu
{

    /**
     * @var string 后台菜单选项配置
     */
    public $itemFile = '@backend/config/menu/items.php';

    /**
     * {@inheritdoc}
     */
    public function run()
    {
        if (empty($this->items))
        {
            $this->items = @include Yii::getAlias($this->itemFile);
        }
        parent::run();
    }

    /**
     * {@inheritdoc}
     */
    protected function normalizeItems($items, & $active)
    {
        foreach ($items as $i => $item)
        {
            if ( ! isset($item['label']))
            {
                $item['label'] = '';
            }
            $encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
            $items[$i]['label'] = $encodeLabel ? Html::encode($item['label']) : $item['label'];
            $items[$i]['icon'] = isset($item['icon']) ? $item['icon'] : '';
            $hasActiveChild = false;
            if (isset($item['items']))
            {
                $items[$i]['items'] = $this->normalizeItems($item['items'], $hasActiveChild);
                if (empty($items[$i]['items']) && $this->hideEmptyItems)
                {
                    unset($items[$i]['items']);
                    if ( ! isset($item['url']))
                    {
                        unset($items[$i]);
                        continue;
                    }
                }
            }
            if ( ! isset($item['active']))
            {
                if ($this->activateParents && $hasActiveChild || $this->activateItems && $this->isItemActive($item))
                {
                    $active = $items[$i]['active'] = true;
                }
                elseif (preg_match('@'.$item['url'][0].'@', $_SERVER['REQUEST_URI']))
                {
                    $active = $items[$i]['active'] = true;
                }
                else
                {
                    $items[$i]['active'] = false;
                }
            }
            elseif ($item['active'])
            {
                $active = true;
            }
            if (isset($item['visible']) && ! $item['visible'])
            {
                unset($items[$i]);
                continue;
            }
        }
        return array_values($items);
    }

    /**
     * {@inheritdoc}
     */
    protected function isItemActive($item)
    {
        if ( ! parent::isItemActive($item))
        {
            if (preg_match('@'.$item['url'][0].'@', $_SERVER['REQUEST_URI']))
            {
                return true;
            }
            if (isset($item['match']))
            {
                if ( ! is_array($item['match']))
                {
                    $item['match'] = [$item['match']];
                }
                foreach ($item['match'] as $matchRule)
                {
                    if (preg_match($matchRule, $_SERVER['REQUEST_URI']))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
        return true;
    }
}
